<?php include("../../includes/includes.php"); ?>
<?php
connect($config);
$parentTradeId = $_GET['parentTradeId'];
$partyTwoReceives = $_GET['partyTwoReceives'];
$partyOneReceives = $_GET['partyOneReceives'];
$loggedInTeam = getLoggedInTeam();

$parentTrade = getTrade($parentTradeId);

$partyOneTeam = getTeam($parentTrade->partyOneId, false);
$partyTwoTeam = getTeamById($parentTrade->partyTwoId, false);

$tradeId = createCounterTrade($parentTradeId, $loggedInTeam->leagueId);

$count = count($partyTwoReceives);
for ($i = 0; $i < $count; $i++) {
	if (substr($partyTwoReceives[$i], 0, 1) == "#") {
		createTradeItem($tradeId, substr($partyTwoReceives[$i], 1), TradeItemType::Pick, PartyType::PartyOne);	
	}
	else {
		createTradeItem($tradeId, $partyTwoReceives[$i], TradeItemType::Player, PartyType::PartyOne);
	}
}

$count = count($partyOneReceives);
for ($i = 0; $i < $count; $i++) {
	if (substr($partyOneReceives[$i], 0, 1) == "#") {
		createTradeItem($tradeId, substr($partyOneReceives[$i], 1), TradeItemType::Pick, PartyType::PartyTwo);	
	}
	else {
		createTradeItem($tradeId, $partyOneReceives[$i], TradeItemType::Player, PartyType::PartyTwo);
	}
}

updateTradeToCounter($parentTrade->id);
mysql_close();

header( 'Location: displayTrades.php' ) ;
?>
